<?php

    class Artigo 
    {
            private $db;
        
            public function __construct()
            {
                $this->db = Zend_Registry::get('db');  
            }
            
            public function create($parameters)
            {  
                /* THE PARAMETERS TO BE INSERT ARE:
                 * 
                 * title
                 * content
                 * nameNode
                 * editability
                 * idOwner
                 * createData
                 * lastModified
                 * aprovedData
                 * idModerator
                 * removedData
                 * idRemover
                 * 
                 */
                
                /* INSERT THE ARTICLE */
                
                $sql =  "INSERT INTO artigo 
                            (title,content,nameNode,editability,eMailOwner,createData,lastModified)
                                VALUES ('".$parameters['title']."','".$parameters['content']."',
                                        '".$parameters['nameNode']."','".$parameters['editability']."',
                                        '".$parameters['eMailOwner']."',NOW(),NOW());";
                
                echo $sql;
                $this->db->query($sql);
                
                /* INSERT THE BOOK */
                
                
               
            }
            
            public function getAritgo()
            {
                
            }
            
            public function showArticles($nodoName)
            {
                $articles  =  $this->getArticles($nodoName);
                
                $return = "";
                
                if(is_array($articles))
                {
                    
                   $return="Mais Votados:<BR>
                            <div id='lastUpdated' style='background-color:green;
                                                        top:200px;
                                                        left:100px;
                                                        postion:absolute;
                                                        width:300px;
                                                        '>";
                   require_once APPLICATION_PATH . "/forms/Forms_HiddenInfo.php";
                   
                    foreach($articles as $article)
                    {
                       $go = new Forms_HiddenInfo();
                       $return.="<br>Nome: ".$article['title'];
                       $return.="Dono: ".$article['name'];
                       $return.="Status: ".$article['editability'];
                       $return.= $go->initialize("Ver", "/artigo/into",array('id' => $article['eMail'],
                                                                         'ownerId' => $article['eMail'],
                                                                          'title' => $article['title'],
                                                                          'content' => $article['content'],
                                                                          'editability' => $article['editability'],
                                                                          'aprovedData' => $article['aprovedData']
                                                                        ));
                    }
                    
                    
                    $return.="</div>";
                }
                    
                echo $return;
                
            }
            
            private function getArticles($nodoName)
            {
                 $sql = "SELECT usuario.name,
                                usuario.eMail AS eMail,
                                artigo.id AS articleId,
                                artigo.title,
                                artigo.content,
                                artigo.editability,
                                artigo.aprovedData
                                    FROM artigo,usuario
                                        WHERE  artigo.nameNode = '$nodoName'
                                        AND artigo.eMailOwner = usuario.eMail;";
                
             
                return $this->db->query($sql)->fetchAll();
            }
            
            
            public function showArticle($parameters)
            {
                 /*THE FORM HEADER
                     array( 'id' => $_POST['id'],
                          'ownerId' => $_POST['ownerId'],
                          'title' => $_POST['title'],
                          'content' => $_POST['content']
                        );
                 */
                
                $id = $parameters['id'];
                $ownerid = $parameters['ownerId'];
                $title = $parameters['title'];
                $content = $parameters['content'];
                          
                $return = "<br><div id='article' style='border:solid;
                                                    border-color:gray;
                                                    position: absolute;
                                                    width: 700px;'>";
                $return.= "<br><br><b>Ttulo: ".$title."</b>";
                $return.= "<br><br>".$content;
                $return.= "</div>";
                
                echo $return;
            }
    }
?>
